import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class StudentSystem {
    private JFrame frame;
    private JTextField tfStudentNo;
    private JPasswordField tfPassword;
    private JTextField tfName;
    private JTextField tfGrade;
    private JTextField tfMajor;
    private JTextField tfContact;

    private JTextField tfLoginStudentNo;
    private JPasswordField tfLoginPassword;

    private String fileName = "students.txt";

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StudentSystem window = new StudentSystem();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public StudentSystem() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 380);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblStudentNo = new JLabel("学号：");
        lblStudentNo.setBounds(30, 27, 88, 23);
        frame.getContentPane().add(lblStudentNo);

        tfStudentNo = new JTextField();
        tfStudentNo.setBounds(113, 27, 259, 23);
        frame.getContentPane().add(tfStudentNo);
        tfStudentNo.setColumns(10);

        JLabel lblPassword = new JLabel("密码：");
        lblPassword.setBounds(30, 60, 88, 23);
        frame.getContentPane().add(lblPassword);

        tfPassword = new JPasswordField();
        tfPassword.setBounds(113, 60, 259, 23);
        frame.getContentPane().add(tfPassword);
        tfPassword.setColumns(10);

        JLabel lblName = new JLabel("姓名：");
        lblName.setBounds(30, 93, 88, 23);
        frame.getContentPane().add(lblName);

        tfName = new JTextField();
        tfName.setBounds(113, 93, 259, 23);
        frame.getContentPane().add(tfName);
        tfName.setColumns(10);

        JLabel lblGrade = new JLabel("年级：");
        lblGrade.setBounds(30, 126, 88, 23);
        frame.getContentPane().add(lblGrade);

        tfGrade = new JTextField();
        tfGrade.setBounds(113, 126, 259, 23);
        frame.getContentPane().add(tfGrade);
        tfGrade.setColumns(10);

        JLabel lblMajor = new JLabel("专业：");
        lblMajor.setBounds(30, 159, 88, 23);
        frame.getContentPane().add(lblMajor);

        tfMajor = new JTextField();
        tfMajor.setBounds(113, 159, 259, 23);
        frame.getContentPane().add(tfMajor);
        tfMajor.setColumns(10);

        JLabel lblContact = new JLabel("联系方式：");
        lblContact.setBounds(30, 192, 88, 23);
        frame.getContentPane().add(lblContact);

        tfContact = new JTextField();
        tfContact.setBounds(113, 192, 259, 23);
        frame.getContentPane().add(tfContact);
        tfContact.setColumns(10);

        JButton btnRegister = new JButton("注册");
        btnRegister.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String studentNo = tfStudentNo.getText().trim();
                String password = new String(tfPassword.getPassword());
                String name = tfName.getText().trim();
                String grade = tfGrade.getText().trim();
                String major = tfMajor.getText().trim();
                String contact = tfContact.getText().trim();

                if (!studentNo.matches("\\d{12}")) {
                    JOptionPane.showMessageDialog(frame, "学号必须是12位数字", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!password.matches("^\\w{6,12}$")) {
                    JOptionPane.showMessageDialog(frame, "密码必须是6-12位字母或数字", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader(fileName));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        String[] parts = line.split(",");
                        if (studentNo.equals(parts[0])) {
                            JOptionPane.showMessageDialog(frame, "学号已经被注册，请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                FileWriter writer = null;
                try {
                    writer = new FileWriter(fileName, true);
                    writer.write(studentNo + "," + password + "," + name + "," + grade + "," + major + "," + contact + "\n");
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    if (writer != null) {
                        try {
                            writer.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                JOptionPane.showMessageDialog(frame, "注册成功");
            }
        });
        btnRegister.setBounds(72, 232, 88, 23);
        frame.getContentPane().add(btnRegister);

        JButton btnReset = new JButton("重置");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tfStudentNo.setText("");
                tfPassword.setText("");
                tfName.setText("");
                tfGrade.setText("");
                tfMajor.setText("");
                tfContact.setText("");
            }
        });
        btnReset.setBounds(234, 232, 88, 23);
        frame.getContentPane().add(btnReset);

        JLabel lblLoginStudentNo = new JLabel("学号：");
        lblLoginStudentNo.setBounds(30, 271, 88, 23);
        frame.getContentPane().add(lblLoginStudentNo);

        tfLoginStudentNo = new JTextField();
        tfLoginStudentNo.setBounds(113, 271, 259, 23);
        frame.getContentPane().add(tfLoginStudentNo);
        tfLoginStudentNo.setColumns(10);

        JLabel lblLoginPassword = new JLabel("密码：");
        lblLoginPassword.setBounds(30, 304, 88, 23);
        frame.getContentPane().add(lblLoginPassword);

        tfLoginPassword = new JPasswordField();
        tfLoginPassword.setBounds(113, 304, 259, 23);
        frame.getContentPane().add(tfLoginPassword);
        tfLoginPassword.setColumns(10);

        JButton btnLogin = new JButton("登录");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String studentNo = tfLoginStudentNo.getText().trim();
                String password = new String(tfLoginPassword.getPassword());

                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader(fileName));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        String[] parts = line.split(",");
                        if (studentNo.equals(parts[0]) && password.equals(parts[1])) {
                            JOptionPane.showMessageDialog(frame, "登录成功");
                            return;
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                JOptionPane.showMessageDialog(frame, "学号或密码不正确", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
        btnLogin.setBounds(72, 340, 88, 23);
        frame.getContentPane().add(btnLogin);

        JButton btnLogout = new JButton("退出");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnLogout.setBounds(234, 340, 88, 23);
        frame.getContentPane().add(btnLogout);
    }
}
